package com.lt.basic.project.application.service.mino.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lt.basic.project.application.mapper.mino.user.UserAuthMapper;
import com.lt.basic.project.application.service.mino.user.IUserAuthService;
import com.lt.basic.project.common.core.PageResult;
import com.lt.basic.project.common.enums.ErrorEnum;
import com.lt.basic.project.common.exception.BaseException;
import com.lt.basic.project.common.util.ConvertUtils;
import com.lt.basic.project.pojo.entity.mino.user.UserAuth;
import com.lt.basic.project.pojo.validate.commons.PageValidate;
import com.lt.basic.project.pojo.validate.mino.user.UserAuthCreateValidate;
import com.lt.basic.project.pojo.validate.mino.user.UserAuthSearchValidate;
import com.lt.basic.project.pojo.validate.mino.user.UserAuthUpdateValidate;
import com.lt.basic.project.pojo.vo.mino.user.UserAuthDetailVo;
import com.lt.basic.project.pojo.vo.mino.user.UserAuthListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户授权表业务接口实现
 * @author LT
 * @version 1.0.0 2023-11-27
 */
@Slf4j
@Service
@Transactional
public class UserAuthServiceImpl implements IUserAuthService {

    @Resource
    private UserAuthMapper userAuthMapper;

    /**
     * 转换VO数据
     * @param userAuth 数据对象
     * @return UserAuthResponse 返回Response对象
     */
    public UserAuthDetailVo getConvertResponse(UserAuth userAuth){
        UserAuthDetailVo detailVo = ConvertUtils.convert(userAuth, UserAuthDetailVo.class);
        return detailVo;
    }

    /**
     * 验证数据
     *
     * @param id 数据id
     * @return
     */
    private UserAuth verifyData(Long id){
    UserAuth userAuth = userAuthMapper.selectById(id);
        if(null == userAuth) {
            throw new BaseException(ErrorEnum.DATA_ERROR.getCode(), ErrorEnum.DATA_ERROR.getMsg());
        }
        return userAuth;
    }

    /**
     * 根据id获取一条VO数据
     * @param id 主键参数
     * @return UserAuthDetailVo
     */
    @Override
    public UserAuthDetailVo detail(Long id) {
        UserAuth userAuth = this.verifyData(id);
        return this.getConvertResponse(userAuth);
    }

    /**
     * 获取列表数据
     * @param searchValidate 查询条件
     * @param pageValidate 分页条件
     * @return PageResult<UserAuthListVo> 响应分页数据
     */
    @Override
    public PageResult<UserAuthListVo> list(PageValidate pageValidate, UserAuthSearchValidate searchValidate) {
        //带SystemAdminRequest类的多条件查询
        LambdaQueryWrapper<UserAuth> lqw = Wrappers.lambdaQuery();
        searchValidate.query(searchValidate, lqw);
        IPage<UserAuth> iPage = userAuthMapper.selectPage(new Page<>(pageValidate.getPageNo(), pageValidate.getPageSize()), lqw);
        List<UserAuthListVo> list = new ArrayList<>();
        iPage.getRecords().forEach(userAuth -> {
            UserAuthListVo vo = ConvertUtils.convert(this.getConvertResponse(userAuth), UserAuthListVo.class);
            list.add(vo);
        });
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 保存
     * @param createValidate 请求参数
     */
    @Override
    public void add(UserAuthCreateValidate createValidate) {
        UserAuth userAuth = new UserAuth();
        userAuth.create(createValidate);
        userAuthMapper.insert(userAuth);
    }

    /**
     * 编辑
     * @param updateValidate 请求参数
     */
    @Override
    public void edit(UserAuthUpdateValidate updateValidate) {
        UserAuth userAuth = this.verifyData(updateValidate.getId());
        userAuth.modify(updateValidate);
        userAuthMapper.updateById(userAuth);
    }

}